<template>
	<view class="wrap">
		<image src="../../static/images/bg.png" class="bg"></image>
		<view class="btn_box">
			<!-- <view class="btn" @tap="login">微信用户一键登录</view> -->
			<!-- <input type="text" v-model="customerPhone" placeholder="请输入手机号" class="ipt"/>
			<input type="text" v-model="code" placeholder="请输入验证码" class="ipt"/>
			<view @tap="getCode" class="getcode">{{t>0?t+'s后重新发送':'获取验证码'}}</view> -->
			<button type="default" class="btn" :loading='loading' @tap="getUSerInfo">微信用户一键登录</button>
			<view type="default" class="btn1" @tap="back">暂不登录</view>
			<!-- <button type="default" class="btn" :loading='loading' @getphonenumber="getphonenumber" open-type='getPhoneNumber' v-else-if="userInfo">微信用户一键登录</button> -->
		</view>
		<view class="agree">登录代表您已同意<text>家教兼职用户协议、隐私协议</text></view>
	</view>
</template>

<script>
	import {toast} from '@/common/js/public.js'
	export default {
		data() {
			return {
				wxcode:'',
				// 字节跳动
				// appid:'tt63cb1576fcf33c4b01',
				// secret:'148cb8e4a91385f3846e9d4ad25fdf24527c742b',
				// 微信
				appid:'wxd10aefdbccaf65fa',
				secret:'784a55733e928e926195b1943757ca2c',
				loading:false,
				openinfo:'',
				// t:0,
				// uuid:'',
				userInfo:'',
				clickFlag:false
			}
		},
		onLoad() {
			uni.setStorageSync('isGoNavigate','1')
			this.userInfo = uni.getStorageSync('tutor_userInfo') || ''
			this.getOpenId()
		},
		onUnload() {
			uni.switchTab({
				url:'/pages/index/index'
			})
			return true
		},
		methods: {
			back() {
				uni.removeStorageSync('isGoNavigate')
				uni.switchTab({
					url:'/pages/index/index'
				})
			},
			getOpenId() {
				uni.login({
				  provider: 'weixin',
				  success: (loginRes) => {
						this.wxcode = loginRes.code
						console.log(loginRes)
						let param = {
							"appid": this.appid,
							"grantType": "authorization_code",
							"jsCode": this.wxcode,
							"secret": this.secret
						}
						this.$api.queryCustomerLoginOpenId(param).then(res=>{
							if(res.code == 1) {
								this.openinfo = res.data
							}
						})
				  },
					fail: (err) => {
						console.log(err)
					}
				});
			},
			getUSerInfo() {
				this.loading = true
				if (this.userInfo) {
					console.log(1)
					this.login()
				} else {
					console.log(2)
					uni.getUserProfile({
						desc: '信息展示', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							// console.log(res)
							uni.setStorageSync('tutor_userInfo',res.userInfo)
							this.$store.commit('set_userInfo',res.userInfo)
							this.userInfo = res.userInfo
							this.login()
						}
					})
				}
			},
			login() {
				this.loading = false
				let param = {
					customerAppId:this.appid,
					customerOpenId:this.openinfo.openid,
					customerUnionId:this.openinfo.unionid
				}
				this.$api.insertCustomerInfo(param).then(res=>{
					let info = res.data || ''
					if (info) {
						let customerInfo = {
							customerAppId:this.appid,
							customerCode:info.customerCode,
							customerOpenId:this.openinfo.openid,
							customerUnionId:this.openinfo.unionid
						}
						uni.setStorageSync('tutor_customerInfo',customerInfo)
						uni.navigateBack()
					} else {
						this.loading = false
					}
				})
			},
			// login() {
			// 	if (this.code == '') {
			// 		toast('请输入验证码')
			// 		return
			// 	}
			// 	if (this.uuid == '') {
			// 		toast('请先获取验证码')
			// 		return
			// 	}
			// 	this.loading = true
			// 	let param = {
			// 		"code": this.code,
			// 		"codeUuid": this.uuid,
			// 		"customerAppId": this.appid,
			// 		"customerOpenId": this.openinfo.openid,
			// 		"customerPhone": this.customerPhone,
			// 		"customerUnionId": this.openinfo.unionid
			// 	}
			// 	this.$api.customerLogin(param).then(res=>{
			// 		if(res.code == 1) {
			// 			this.loading = false
			// 			uni.setStorageSync('tutor_customerCode',res.data.customerCode)
			// 			uni.navigateBack({
							
			// 			})
			// 		}
			// 	})
			// },
			// 发送验证码
			// getCode(){
			// 	if (this.t>0) return
			// 	uni.hideKeyboard()
			// 	var myreg=/^1[0-9]{10}$/
			// 	if (!myreg.test(this.customerPhone)) {
			// 		toast('手机号格式不正确')
			// 		return;
			// 	}
			// 	let param = {
			// 		"customerAppId": this.appid,
			// 		"customerOpenId": this.openinfo.openid,
			// 		"customerPhone": this.customerPhone,
			// 		"customerUnionId": this.openinfo.unionid
			// 	}
			// 	this.$api.queryCustomerLoginVerificationCode(param).then(res=>{
			// 		if(res.code==1){
			// 			console.log(res.data)
			// 			this.t = 60
			// 			this.uuid = res.data.uuid
			// 			setTimeout(() => {
			// 				this.countdown()
			// 			}, 1000)
			// 		}else{
			// 			toast(res.message)
			// 		}
			// 	})
			// },
			// 倒计时
			// countdown(){
			// 	if(this.t > 0){
			// 		this.t--
			// 	}else{
			// 		return
			// 	}
			// 	setTimeout(() => {
			// 		this.countdown()
			// 	}, 1000)
			// },
		}
	}
</script>

<style lang="stylus" scoped>
	.wrap
		width 100%
		height 100vh
		position relative
	.bg
		width 100%
		height 628rpx
	.getcode
		margin 20rpx 0
	.btn_box
		width 100%
		box-sizing border-box
		padding 0 30rpx
		margin-top 170rpx
		.btn
			width 100%
			height 88rpx
			line-height 88rpx
			background-color #34BE97
			color #fff
			border none
		.btn1
			width 100%
			height 88rpx
			line-height 88rpx
			border-radius 40rpx
			font-size 28rpx
			border 2rpx solid #d8d8d8
			margin-top 40rpx
			text-align center
	.tips
		font-size 24rpx
		color #999
	.agree
		position absolute
		left 0
		bottom 50rpx
		font-size 26rpx
		color #999
		width 100%
		text-align center
		text
			color #34BE97
</style>
